<?php

include_once 'database.php';
// load ini files
$ini_param = parse_ini_file("param.ini");
$ini_str = parse_ini_file($ini_param['file_strings']);

// initialize the DOM object that will store the XML
$xmlDoc = new DOMDocument();

// Read calling parameter
$q=$_GET["q"];
echo 'parameter: '.$q;
// connect to the database
Database::initDB();

// load feeds
// retrieve feeds from the database
$query = "SELECT URL FROM ".$ini_param['feed_source_table'];
$rss_resource = mysql_query($query);

if ($rss_resource==false)
{echo " thn pathsame arxhge ". mysql_error()." ";}

$num_rows = mysql_num_rows($rss_resource);

$rss_feed_list = array();
$rss_feed_line = array();

for ($i=0; $i<$num_rows; $i++)
{
	//get an associative array of data for this entry
	$rss_feed_line = mysql_fetch_assoc($rss_resource);
	
	//add the URL for this feed in the array of URLs
	//$rss_feed_list[] = html_entity_decode($rss_feed_line['URL'], ENT_QUOTES, "UTF-8");
	$rss_feed_list[] = $rss_feed_line['URL'];
}



/*
// read the feeds from the database
// hardcoded for now*/
$rss_feed_list = array(	'http://news.google.com/news?ned=us&topic=h&output=rss',
						'http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml',
						'http://www.newsit.gr/rss/artrss.php');

// iterate through the feeds
foreach ($rss_feed_list as $url)
{
	//echo $url;
	// read XML from current feed
	$xmlDoc->load($url);
	$x=$xmlDoc->getElementsByTagName('item');	
	$count = $x->length;
	for ($i=0; $i<=3; $i++)
	{	
		$item_pubDate=$x->item($i)->getElementsByTagName('pubDate')
		->item(0)->childNodes->item(0)->nodeValue;   
		$item_title=$x->item($i)->getElementsByTagName('title')
		->item(0)->childNodes->item(0)->nodeValue;
		$item_link=$x->item($i)->getElementsByTagName('link')
		->item(0)->childNodes->item(0)->nodeValue;
		$item_desc=$x->item($i)->getElementsByTagName('description')
		->item(0)->childNodes->item(0)->nodeValue;

	
		//need to replace single with double or the other way around? Error in sql query, so the doubles are the issue

		$item_pubDate = htmlentities($item_pubDate, ENT_QUOTES, "UTF-8");
		$item_title = htmlentities($item_title, ENT_QUOTES, "UTF-8");
		$item_link = htmlentities($item_link, ENT_QUOTES, "UTF-8");
		$item_desc = htmlentities($item_desc, ENT_QUOTES, "UTF-8");	


		//$sql = sprintf("SELECT FROM %s WHERE LINK='%s'",$ini_param['item_table'],$item_link);
		$sql = 'SELECT * FROM '.$ini_param['item_table'].' WHERE LINK="'.$item_link.'"';	
		$duplicate_check = mysql_query($sql);

		if(mysql_num_rows($duplicate_check)==0)
		{
			//add the data to the database, I hope  
			$sql = 'INSERT INTO '.$ini_param['item_table'].' (PUBDATE, TITLE, LINK, DESCRIPTION) VALUES ("'.$item_pubDate.'","'.$item_title.'","'.$item_link.'","'.$item_desc.'")';
			if (mysql_query($sql))
			{
				//echo "Added data successfully\n";
			}
			else
			{
				// this is bad stuff
				echo 'Error adding data: ' . mysql_error() . "\n";
				echo 'SQL query: ' . $sql . "\n";
			}
		}
		else
		{
			// do logging output
		}

	}
}

// retrieve items from the database
$rss_resource = mysql_query("SELECT PUBDATE, TITLE, LINK, DESCRIPTION FROM ".$ini_param['item_table']);
$num_rows = mysql_num_rows($rss_resource);

for ($i=0; $i<$num_rows; $i++)
{

	//echo 'Item '.$i;
	$rss_item = mysql_fetch_assoc($rss_resource);

	$rss_item['PUBDATE']	= html_entity_decode($rss_item['PUBDATE'], 		ENT_QUOTES, "UTF-8");
	$rss_item['TITLE']		= html_entity_decode($rss_item['TITLE'], 		ENT_QUOTES, "UTF-8");
	$rss_item['LINK']		= html_entity_decode($rss_item['LINK'], 		ENT_QUOTES, "UTF-8");
	$rss_item['DESCRIPTION']= html_entity_decode($rss_item['DESCRIPTION'],	ENT_QUOTES, "UTF-8");		
	
	//print_r($rss_item);
	
	echo ("<p><br /><a href='" . $rss_item['LINK']
	. "'>" . $rss_item['TITLE'] . "</a> ".$rss_item['PUBDATE']);
	echo ("<br />");
	echo ($rss_item['DESCRIPTION'] . "</p>");

}
?> 